import React, { Component } from 'react'
import { withRouter } from 'react-router'

/**
 * 该组件不提供然后内容，只是提供阻塞 
 * @class Prompt
 * @extends {Component}
 */
class _Prompt extends Component {
    static defaultProps = {
        when: false, // true 添加阻塞·
        message: '' // 阻塞时提示内容
    }

    componentDidMount() {
        this.handleBlock()
    }
    componentWillUnmount() {
        this.clearBlock()
    }
    componentDidUpdate() { // 当属性发生变化时
        this.handleBlock()
    }
    render = () => null

    handleBlock() {
        const { when, message, history } = this.props
        if (when) {
            this.unBlock = history.block(message)
        } else {
            this.clearBlock()
        }
    }
    clearBlock() {
        this.unBlock && this.unBlock()
    }
}

export default withRouter(_Prompt)